package com.example.mutildatasource.controller;

import com.example.mutildatasource.entity.TreeNode;
import com.example.mutildatasource.entity.User;
import com.example.mutildatasource.mapper.secondary.UserMapper;
import com.example.mutildatasource.service.UserService;
import com.example.mutildatasource.service.ZtreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("zTree")
public class ZtreeController {

    @Autowired
    private ZtreeService ztreeService;

    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

 /*   @ResponseBody
    @RequestMapping("/initData")
    public List initTreeData(){
        List<Tree> treeList = new ArrayList<Tree>();
        for(int i=1;i<10;i++){
            Tree tree1 = new Tree(String.valueOf(0),String.valueOf(i),"一级菜单"+i);
            treeList.add(tree1);
            for(int j=1;j<10;j++){
                Tree tree2 = new Tree(String.valueOf(i),String.valueOf(i)+String.valueOf(j),"二级菜单"+String.valueOf(i)+String.valueOf(j));
                treeList.add(tree2);
                for(int z=1;z<10;z++){
                    Tree tree3 = new Tree(String.valueOf(i)+String.valueOf(j),String.valueOf(i)+String.valueOf(j)+String.valueOf(z),"三级节点"+String.valueOf(i)+String.valueOf(j)+String.valueOf(z));
                    treeList.add(tree3);
                }
            }
        }
*//*        Tree tree1 = new Tree(String.valueOf(0),String.valueOf(1),"test1");
        Tree tree2 = new Tree(String.valueOf(1),String.valueOf(11),"test11");
        Tree tree3 = new Tree(String.valueOf(1),String.valueOf(12),"test11");
        Tree tree4 = new Tree(String.valueOf(11),String.valueOf(111),"test111");
        treeList.add(tree1);
        treeList.add(tree2);
        treeList.add(tree3);
        treeList.add(tree4);*//*
        return treeList;
    }*/

    @RequestMapping("/initData")
    public List< ? super TreeNode> buildTree(){
        return this.ztreeService.buildTree();
    }

    @RequestMapping("/asynchInitData")
    public List< ? super TreeNode> asynchBuildTree(String treeId,String isParent){
        return this.ztreeService.asynchBuildTree(treeId,isParent);
    }

    @RequestMapping("/getAllUser")
    public List<User> getAllByCondition(){
        User user  = new User();
        List<User> userList = this.userMapper.getAllByCondition(user);
        return this.userMapper.getAllByCondition(user);
    }

//    @RequestMapping("/insertUser")
//    @Transactional(value = "secondaryTransactionManager")

    public void insertUser(){
        this.userService.insertUser();
//        try {
//            User user1 = new User("zhangsan","root","男");
//            this.userMapper.insertUser(user1);
//
////            测试事务，user表用户名不能为空
//            User user2 = new User();
////            user2.setUserName("Lucy");
//            user2.setPassword("root");
//            user2.setSex("女");
//            this.userMapper.insertUser(user2);
//
//            User user3 = new User("lisi","root","男");
//            this.userMapper.insertUser(user3);
//
//        }catch (Exception e){
//            throw new RuntimeException(e.toString());
//        }
    }

    public static void main(String[] args) {
        Assert.isTrue(1>2,"1大于2为假");
    }

}
